<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-input style="marginBottom: 15px" v-model="roleName" placeholder="请输入角色名"></el-input>
      <el-cascader-panel v-model="cascader" :props="props" :options="options"></el-cascader-panel>
      <span slot="footer" class="dialog-footer">
        <el-button @click="no">取 消</el-button>
        <el-button type="primary" @click="yes">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      defaultL: "添加角色"
    }
  },
  data() {
    return {
      id: 0,
      cascader: null,
      roleName: '',
      props: {
        multiple: true
      },
      dialogVisible: false,
      accessList: [
        {
          id: 130200,
          name: "角色管理",
          tag: "system.role",
          url: null,
          parentId: 130000
        },
        {
          id: 130100,
          name: "用户管理",
          tag: "system.user",
          url: null,
          parentId: 130000
        },
        { id: 130000, name: "系统维护", tag: "system", url: null, parentId: 0 },
        {
          id: 120200,
          name: "停用词维护",
          tag: "basicData.stopWords",
          url: null,
          parentId: 120000
        },
        {
          id: 120100,
          name: "发布机构",
          tag: "basicData.publishOrgan",
          url: null,
          parentId: 120000
        },
        {
          id: 120000,
          name: "基础数据维护",
          tag: "basicData",
          url: null,
          parentId: 0
        },
        {
          id: 110200,
          name: "词表列表",
          tag: "word.list",
          url: null,
          parentId: 110000
        },
        {
          id: 110100,
          name: "词表框架",
          tag: "word.frame",
          url: null,
          parentId: 110000
        },
        { id: 110000, name: "词表管理", tag: "word", url: null, parentId: 0 },
        {
          id: 100500,
          name: "政策修改",
          tag: "policy.modify",
          url: null,
          parentId: 100000
        },
        {
          id: 100400,
          name: "政策数据加工",
          tag: "policy.process",
          url: null,
          parentId: 100000
        },
        {
          id: 100300,
          name: "手工数据导入",
          tag: "policy.import",
          url: null,
          parentId: 100000
        },
        {
          id: 100100,
          name: "采集数据入库",
          tag: "policy.list",
          url: null,
          parentId: 100000
        },
        {
          id: 100000,
          name: "政策信息管理",
          tag: "policy",
          url: null,
          parentId: 0
        }
      ],
      options: [
        {
          value: 100000,
          label: "政策信息管理",
          children: [
            {
              value: 100100,
              label: "采集数据入库"
            },
            {
              value: 100300,
              label: "手工数据导入"
            },
            {
              value: 100400,
              label: "政策数据加工"
            },
            {
              value: 100500,
              label: "政策修改"
            }
          ]
        },
        {
          value: 110000,
          label: "词表管理",
          children: [
            {
              value: 110100,
              label: "词表框架",
            },
            {
              value: 110200,
              label: "词表列表"
            }
          ]
        },
        {
          value: 120000,
          label: "基础数据维护",
          children: [
            {
              value: 120100,
              label: "发布机构"
            },
            {
              value: 120200,
              label: "停用词维护"
            }
          ]
        },
        {
          value: 130000,
          label: "系统维护",
          children: [
            {
              value: 130100,
              label: "用户管理"
            },
            {
              value: 130200,
              label: "角色管理"
            }
          ]
        }
      ]
    };
  },
  methods: {
    no() {
      this.dialogVisible = false;
    },
    yes() {
      if (!this.roleName) {
        this.$message({
            message: '请输入角色名',
            type: 'warning'
        })
        return
      }
      if (!this.cascader || this.cascader.length === 0) {
        this.$message({
            message: '请选择权限',
            type: 'warning'
        })
        return
      }
      var arr = []
      for (var i = 0; i < this.cascader.length; i++) {
        arr.push(this.cascader[i][0])
        arr.push(this.cascader[i][1])
      }
      arr = Array.from(new Set(arr))
      if (this.title === '添加角色') {
        this.axios.post('http://81.70.46.38:8080/role/addOrUpdate', {
          name: this.roleName,
          accessList: arr
        }, {
          withCredentials: true
        }).then(res => {
          if (res.data.code === 200) {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }
          this.roleName = ''
          this.cascader = []
          this.dialogVisible = false;
          this.$emit('getList')
        })
      } else {
        this.axios.post('http://81.70.46.38:8080/role/addOrUpdate', {
          name: this.roleName,
          id: this.id,
          accessList: arr
        }, {
          withCredentials: true
        }).then(res => {
          if (res.data.code === 200) {
            this.$message({
              message: '更新成功',
              type: 'success'
            })
          }
          this.roleName = ''
          this.cascader = []
          this.dialogVisible = false;
          this.$emit('getList')
        })
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
